package com.aretias.jkholdings.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.aretias.jkholdings.model.Category;

/**
 * 
 * @author ryu
 *
 */
@Controller
public class CategoryController extends BaseController {
	
	/**
	 * 
	 * @param categoryId
	 * @return
	 */
	@RequestMapping("/category/getChilds/{categoryId}.do")
	public @ResponseBody ModelAndView getChilds(@PathVariable("categoryId") Integer categoryId) {
		Category category = new Category();
		category.setParentCategoryId(categoryId);
		List<Category> categoryList = categoryService.getChild(category);
		return new ModelAndView("pageJsonReport", "category", categoryList);
	}
	
	/**
	 * 
	 * @param param
	 * @param session
	 * @return
	 */
	@RequestMapping("/category.do")
	public ModelAndView showCategoryManager(Category param, HttpSession session) {
		ModelAndView mav = new ModelAndView();
		int defaultSecondDeptsParentsId = 0;
		if(getUserForSession(session) != null){
			
			List<Category> firstDeptCategoryList = getFirstDeptCategoryList();
			if(categoryService.getFirstCategory() != null){
				defaultSecondDeptsParentsId = categoryService.getFirstCategory().getCategoryId();
				param.setParentCategoryId(defaultSecondDeptsParentsId);
			}
			List<Category> secondDeptCategoryList = categoryService.getChild(param);
			
			mav.addObject("firstDeptCategoryList", firstDeptCategoryList);
			mav.addObject("secondDeptCategoryList", secondDeptCategoryList);
			mav.addObject("categoryList", firstDeptCategoryList);
			mav.setViewName("category/categoryManager");
			
		}else{
			mav.setViewName("redirect:/home.do");
		}
		return mav;
	}
	
	/**
	 * 
	 * @param param
	 * @param session
	 * @return
	 */
	@RequestMapping( value = "/category/remove.do" , method = RequestMethod.POST)
	public ModelAndView removeCategory(Category param, HttpSession session){
		ModelAndView mav = new ModelAndView();
		if(getUserForSession(session) != null){
			categoryService.removeCategory(param);
			if(param.getParentCategoryId() == 0){
				categoryService.removeChildCategory(param);
			}
			mav.setViewName("redirect:/category.do");
		}else{
			mav.setViewName("redirect:/home.do");
		}
		return mav;
	}
	
	/**
	 * 
	 * @param param
	 * @param session
	 * @return
	 */
	@RequestMapping( value="/category/add.do" ,method=RequestMethod.POST)
	public ModelAndView addCategory(Category param, HttpSession session){
		ModelAndView mav = new ModelAndView();
		if(getUserForSession(session) != null){
			categoryService.addCategory(param);
			mav.setViewName("redirect:/category.do");
		}else{
			mav.setViewName("redirect:/home.do");
		}
		return mav;
	}
}
